﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Bussiness;
using System.Text;
namespace EWebsite.Admin {
    public partial class Category2 : System.Web.UI.Page {
        private DataTable _tblCategories;
        public DataTable TblCategories { 
            get { return this._tblCategories; } 
        }
        public DataTable temp;


        public Category2() {
            //CategoryBuss buss = new CategoryBuss();
            //TblCategories = buss.getCategories();
        }

        protected void Page_Load(object sender, EventArgs e) {          
            if(!IsPostBack)
                _DataBind();            
        }

        protected void _DataBind() {
            CategoryBuss buss = new CategoryBuss();
            _tblCategories = buss.getCategories();
            grvCategories.DataSource = _tblCategories;
            grvCategories.DataBind();
            grvCategories.DataKeyNames = new String[] { "CatID" };
        }

        protected void grvCategories_RowDeleting(object sender, GridViewDeleteEventArgs e) {
            //read category id
            Label lblCatID = (Label)grvCategories.Rows[e.RowIndex].FindControl("lblCatID");
            int catID = int.Parse(lblCatID.Text);

            //delete category
            CategoryBuss catbuss = new CategoryBuss();
            catbuss.delete(catID);

            _DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e) {
            if (txtCatName.Text.Trim().Equals("")) {
                return;
            }
            int parentID = int.Parse(Request["categoriesTree"]);
            if (parentID < 0) {
                parentID = 0;
            }
            CategoryBuss buss = new CategoryBuss();
            buss.add(txtCatName.Text.Trim(), parentID);

            _DataBind();
        }

        protected DataTable getCategoriesTable() {
            DataTable table = new DataTable();
            table.Columns.Add("CatID");
            table.Columns.Add("CatName");
            table.Columns.Add("ParentCatID");
            DataRow  row = table.NewRow();
            row[0] = 0;
            row[1] = "--No parent--";
            row[2] = 0;
            
            table.Rows.Add(row);
            
            DataTable temp = _tblCategories;
            printCategoryTree2(ref temp, 0, 0, table);
            return table;
        }

        protected void grvCategories_RowEditing(object sender, GridViewEditEventArgs e) {
            grvCategories.EditIndex = e.NewEditIndex;
            _DataBind();
        }

        protected void grvCategories_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {
            grvCategories.EditIndex = -1;
            _DataBind();
        }

        protected void grvCategories_RowUpdating(object sender, GridViewUpdateEventArgs e) {
            int index = e.RowIndex;

            int catID = int.Parse(((Label)grvCategories.Rows[index].FindControl("lblCatID")).Text);
            int parentID = int.Parse(((DropDownList)grvCategories.Rows[index].FindControl("drpParentCatID")).SelectedValue);
            String catName = ((TextBox)grvCategories.Rows[index].FindControl("txtCatName")).Text.Trim();

            //break if name is empty
            if (catName.Equals(""))
                return;

            CategoryBuss buss = new CategoryBuss();
            buss.update(catID, catName, parentID);

            grvCategories.EditIndex = -1;
            _DataBind();
        }

        protected void grvCategories_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            grvCategories.PageIndex = e.NewPageIndex;
            _DataBind();
        }

        protected void printCategoryTree2(ref DataTable categories, int start, int level, DataTable tblCat) {
            foreach (DataRow child in categories.Rows) {
                if (child["ParentCatID"].ToString() == start.ToString()) {

                    //Response.Write(new String('-', level) + child["CatName"].ToString() + "<br/>");
                    DataRow row = tblCat.NewRow();
                    row[0] = child[0];                    
                    row[1] = new String('-', level) + child[1].ToString();
                    row[2] = child[2];
                    tblCat.Rows.Add(row);
                    printCategoryTree2(ref categories, int.Parse(child["CatID"].ToString()), level + 1, tblCat);
                }
            }
        }

        protected void printCategoryTree(ref DataTable categories, int start, int level) {
            bool hasChild = false;
            foreach (DataRow child in categories.Rows) {
                if (child["ParentCatID"].ToString() == start.ToString()) {

                    if (hasChild == false) {
                        hasChild = true;

                        Response.Write("<ul>");
                    }
                    Response.Write("<li><a href=\"#\">" + child["CatName"] + "</a>");
                    //Response.Write(new String('-', level) + child["CatName"].ToString() + "<br/>");
                    printCategoryTree(ref categories, int.Parse(child["CatID"].ToString()), level + 1);

                    Response.Write("</li>");
                }
            }

            if (hasChild) Response.Write("</ul>");
        }

    }
}